﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BenhVien
{
    public partial class CapVT : Form
    {
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        Connect conn = new Connect();
        public CapVT()
        {
            InitializeComponent();
        }
        public void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select * from CapVatTu";
            da.SelectCommand = command;
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            if (txtMP.Text == "" | txtTP.Text == "" | txtMVT.Text == "" | txtSL.Text == "" | dateTimePicker1.Text == "")
                MessageBox.Show("Bạn Chưa Nhập Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK);
            else
            {
                if (check("Select MaVT from KhoVatTu where MaVT =@DoiSo", txtMP.Text) == true)
                    MessageBox.Show("Ma bi trung", "Loi", MessageBoxButtons.OK);
                else
                {
                    DataRow row = dt.NewRow();
                    row["MaPhong"] = txtMP.Text;
                    row["TenPhong"] = txtTP.Text;
                    row["MaVT"] = txtMVT.Text;
                    row["NgayCap"] = dateTimePicker1.Text;
                    row["SoLuong"] = txtSL.Text;
                    dt.Rows.Add(row);
                    SqlCommand commandInsert = new SqlCommand();
                    commandInsert.Connection = conn.con;
                    commandInsert.CommandType = CommandType.Text;
                    commandInsert.CommandText = @"Insert CapVatTu (MaPhong, TenPhong, MaVT, NgayCap, SoLuong) Values 
                                                        (@MaPhong, @TenPhong, @MaVT, @NgayCap, @SoLuong)";
                    commandInsert.Parameters.Add("@MaPhong", SqlDbType.NVarChar, 50, "MaPhong");
                    commandInsert.Parameters.Add("@TenPhong", SqlDbType.NVarChar, 50, "TenPhong");
                    commandInsert.Parameters.Add("@MaVT", SqlDbType.NVarChar, 50, "MaVT");
                    commandInsert.Parameters.Add("@NgayCap", SqlDbType.Date, 50, "NgayCap");
                    commandInsert.Parameters.Add("@SoLuong", SqlDbType.Int, 50, "SoLuong");
                    da.InsertCommand = commandInsert;
                    da.Update(dt);
                    MessageBox.Show("Ban da them Thanh Cong", "chuc Mung", MessageBoxButtons.OK);
                }
            }
        }
        private bool check(String s, String s1)
        {
            bool check1 = false;
            DataTable data = new DataTable();
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = s;
            command.Parameters.Add("@DoiSo", SqlDbType.NVarChar, 50).Value = s1;
            da.SelectCommand = command;
            da.Fill(data);
            if (data.Rows.Count > 0) check1 = true;
            return check1;
        }
        private void CapVT_Load(object sender, EventArgs e)
        {
            conn.connect();
            getdata();
        }
    }
}
